package com.xgc.erp.order.purchase.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.model.PurchaseOrder;
import com.xgc.common.routes.ControllerBind;
import com.xgc.common.visit.Visitor;
import com.xgc.common.vo.Feedback;
import com.xgc.erp.order.purchase.service.PurchaseOrderService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ControllerBind(path = "/erp/order/purchase")
public class PurchaseOrderController extends BaseController {


    @Inject
    PurchaseOrderService service;

    public void index() {
        setAttr("orderStatusMap", Constan.ORDER_STATUS_MAP);
        render("index.html");
    }

    /**
     * 表格数据
     */
    public void list(){
        Record record = new Record();
        String sql = Db.getSql("erp.getPurchaseList");
        renderJson(service.queryForList(sql,getParaToInt("pageNumber",1), getParaToInt("pageSize",10),record,"order by create_time desc"));
    }

    /**
     * 新增页面
     */
    public void add() {
        setAttr("orderStatusMap", Constan.ORDER_STATUS_MAP);
        render("add.html");
    }

    public void save(){
        PurchaseOrder order = getBean(PurchaseOrder.class);
        order.setOrderId(service.getPrimaryKey(Constan.SEQ_PURCHASE_ORDER));
        order.setStatus(Constan.STATUS_1000);
        Visitor vs = getVisitor();
        order.setCreator(vs.getCode());
        order.setCreateTime(new Date());
        if(!order.save()){
            setException("数据插入失败");
        }
        render("add.html");
    }

    /**
     * 编辑页面
     */
    public void edit(){
        String orderId = get("orderId");
        Record record = new Record();
        record.set("order_id",orderId);
        PurchaseOrder purchaseOrder = (PurchaseOrder)service.findById(orderId);
        setAttr("purchaseOrder", purchaseOrder);
        setAttr("orderStatusMap", Constan.ORDER_STATUS_MAP);
        render("edit.html");
    }

    public void update(){
        PurchaseOrder order = getBean(PurchaseOrder.class);
        Visitor vs = getVisitor();
        order.setModifier(vs.getCode());
        order.setUpdateTime(new Date());
        if(!order.update()){
            setException("数据插入失败");
        }
        render("edit.html");
    }

    /**
     * 采购单预览
     */
    public void review(){
        String orderId = get("orderId");
        PurchaseOrder order = (PurchaseOrder)service.findById(orderId);
        setAttr("order", order);
        render("review.html");
    }

    public void delete() {
        service.deleteByIds(getIds());
        renderJson(Feedback.success());
    }

    /**
     * 通过,支持批量操作
     */
    public void approve(){
        List<String> ids = getIds();
        List<Integer> idList = new ArrayList<>();
        for(String id:ids){
            idList.add(Integer.parseInt(id));
        }
        Visitor vs = getVisitor();
        service.modifyStatus(idList,Constan.STATUS_1002,vs.getCode());
        renderJson(Feedback.success());
    }

    /**
     * 退回，支持批量操作
     */
    public void refuse(){
        List<String> ids = getIds();
        List<Integer> idList = new ArrayList<>();
        for(String id:ids){
            idList.add(Integer.parseInt(id));
        }
        Visitor vs = getVisitor();
        service.modifyStatus(idList,Constan.STATUS_1003,vs.getCode());
        renderJson(Feedback.success());
    }
}
